package com.example.localareanetworkchat.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.example.localareanetworkchat.MainActivity
import com.example.localareanetworkchat.R
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class LoginActivity : AppCompatActivity() {

    private var viewModel: LoginViewModel ?= null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        viewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application)).get(LoginViewModel::class.java)

        btn_chat_login_register.setOnClickListener{
            val name = et_chat_login_name.text.toString()
            val password = et_chat_login_password.text.toString()
            val port = et_chat_login_port.text.toString()

            if (viewModel?.checkLoginTextContent(name)?.or(viewModel?.checkLoginTextContent(password)!!) == true){
                Toast.makeText(this@LoginActivity, "用户名或密码错不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            lifecycleScope.launch(Dispatchers.IO) {
                val duplicated = viewModel?.checkDuplicatedUser(name)
                lifecycleScope.launch(Dispatchers.Main){
                    if (duplicated == true){
                        Toast.makeText(this@LoginActivity, "用户名已存在", Toast.LENGTH_SHORT).show()
                    }else {
                        lifecycleScope.launch(Dispatchers.IO) {
                            val user = viewModel?.insertUser(name, password)

                            lifecycleScope.launch(Dispatchers.Main){
                                val intent = Intent(this@LoginActivity, MainActivity::class.java)
                                val bundle = Bundle()
                                bundle.putParcelable("user", user)
                                bundle.putString("port", port)
                                intent.putExtras(bundle)
                                startActivity(intent)
                            }
                        }
                    }
                }
            }
        }

        btn_chat_login_login.setOnClickListener {
            val name = et_chat_login_name.text.toString()
            val password = et_chat_login_password.text.toString()
            val port = et_chat_login_port.text.toString()

            if (viewModel?.checkLoginTextContent(name)?.or(viewModel?.checkLoginTextContent(password)!!) == true){
                Toast.makeText(this@LoginActivity, "用户名或密码错不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            lifecycleScope.launch(Dispatchers.IO) {
                val user = viewModel?.checkLogin(name, password)

                lifecycleScope.launch(Dispatchers.Main) {
                    if (user != null) {
                        val intent = Intent(this@LoginActivity, MainActivity::class.java)
                        val bundle = Bundle()
                        bundle.putParcelable("user", user)
                        bundle.putString("port", port)
                        intent.putExtras(bundle)
                        startActivity(intent)

                    } else {
                        Toast.makeText(this@LoginActivity, "用户名不存在或密码错误", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }
}